package com.yxuan.springboot.controller;

import com.yxuan.springboot.model.AdminLogin;
import com.yxuan.springboot.model.UserLogin;
import com.yxuan.springboot.model.UserVip;
import com.yxuan.springboot.service.AdminLoginService;
import com.yxuan.springboot.service.UserInformationService;
import com.yxuan.springboot.service.UserLoginService;
import com.yxuan.springboot.service.UserVipService;
import com.yxuan.springboot.util.DaysCalculateUtil;
import com.yxuan.springboot.util.ResolveTokenUtil;
import com.yxuan.springboot.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import com.yxuan.springboot.model.UserInformation;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author zchao
 *   UserInformation控制器
 * @date 2023-05-09 11:17:05
 */
@Controller
@Api(tags = "用户信息表")
@RequestMapping(value = "/userinformation", method = {RequestMethod.POST,RequestMethod.GET})
public class UserInformationController extends BaseController<UserInformation>{

    @Autowired
    private UserInformationService userInformationService;
    @Autowired
    private UserLoginService userLoginService;


    /**
     * 添加
     */
    @ApiOperation(value = "添加数据", notes = "添加数据")
    @RequestMapping("/save")
    @ResponseBody
    public Result save(HttpServletRequest request, @RequestBody UserInformation userInformation) throws IOException {
        try {

            UserLogin userLogin = ResolveTokenUtil.targetLogin(request.getHeader("token"));
            UserLogin targetUserLogin = userLoginService.findObjectByPramas(userLogin);
            userInformation.setUserId(targetUserLogin.getId());

            return new Result(baseService.save(userInformation),SUCCESSCODE,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(FAILCODE,"添加失败");
        }
    }


    @Autowired
    private UserVipService userVipService;
    @Autowired
    private AdminLoginService adminLoginService;
    /**
     * 修改
     */
    @ApiOperation(value = "修改数据", notes = "修改数据")
    @RequestMapping("/upd")
    @ResponseBody
    public Result upd(HttpServletRequest request,@RequestBody UserInformation userInformation) throws Exception {
        try {

            UserLogin userLogin = ResolveTokenUtil.targetLogin(request.getHeader("token"));
            UserLogin targetUserLogin = userLoginService.findObjectByPramas(userLogin);

            userInformation.setUserId(targetUserLogin.getId());
            UserInformation userInformation1 = new UserInformation();
            userInformation1.setUserId(targetUserLogin.getId());
            UserInformation objectByPramas = userInformationService.findObjectByPramas(userInformation1);
            double v = DaysCalculateUtil.mytest15(objectByPramas.getUpdTime(), new Date());

            AdminLogin adminLogin = new AdminLogin();
            adminLogin.setId(4);
            AdminLogin targetAL = adminLoginService.findObjectByPramas(adminLogin);
            if (targetAL.getUserName().equals("开")){
                //如果为开，则不修改次数，直接进行userInformation的修改
                return new Result(baseService.upd(userInformation),SUCCESSCODE,"修改成功");
            }

            UserVip userVip = new UserVip();
            userVip.setUserId(targetUserLogin.getId());

            UserVip userVip1 = userVipService.findObjectByPramas(userVip);
            String updTimes = userVip1.getInfoUpdTimes();
            if(userVip1.getInfoUpdTimes().equals("NaN")){
                updTimes = "3";
            }
            if(Integer.valueOf(updTimes)<=0){
                Map<String, String> map = new HashMap<>();
                map.put("state","fail");
                return new Result(map,FAILCODE,"修改次数已用完");
            }

            //设置当前修改的时间
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            Date updTime = formatter.parse(formatter.format(date));
            userInformation.setUpdTime(updTime);
            //将修改个人信息次数减1
            int updNum = Integer.parseInt(updTimes);
            updNum-=1;
            userVip.setInfoUpdTimes(String.valueOf(updNum));
            userVipService.upd(userVip);


            return new Result(baseService.upd(userInformation),SUCCESSCODE,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(FAILCODE,"修改失败");
        }
    }


    /**
     * 根据条件查询单个结果
     *
     */
    @ApiOperation(value = "查询用户个人信息", notes = "查询用户个人信息")
    @RequestMapping("/findUserInfo")
    @ResponseBody
    public Result loadObjectByPramas(HttpServletRequest request){
        try {
            UserLogin userLogin = ResolveTokenUtil.targetLogin(request.getHeader("token"));
            UserLogin targetUserLogin = userLoginService.findObjectByPramas(userLogin);
            UserInformation userInformation = new UserInformation();
            userInformation.setUserId(targetUserLogin.getId());

            return new Result(baseService.findObjectByPramas(userInformation),SUCCESSCODE,"查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(FAILCODE,"查询失败");
        }
    }


}
